package ebcs.scan.controller.capacityCtr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import ebcs.database.bean.BsCapacityAll;
import ebcs.database.bean.BsCapacityDstbt;
import ebcs.database.bean.BsUserInfor;
import ebcs.database.service.BsCapacityDstbtServiceImp;
import ebcs.database.service.BsUserInforServiceImp;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.string.DateUtil;
import jplat.tools.string.JRandomUtil;
 
/**
 * 
 * @author chenzhuo
 * @date 2017年8月16日
 * <p>Title:CapacityManagerCtrl </p>
 * <p>Description:产能分配及统计类</p>
 * <p>Company:Hunan link-us </p>
 */
@Controller
public class CapacityManagerCtrl extends JAppBaseService
{
	private Logger logger = Logger.getLogger(CapacityManagerCtrl.class);
	@Autowired
	BsCapacityDstbtServiceImp capService ;  
	
	@Autowired
	BsUserInforServiceImp userInforService;
	/**
	 * 查询需求内的用户列表
	 */
	@RequestMapping("/page/cap/{needId}/selectUserInforInNeedInfor.do")
	public void selectUserInforInNeedInfor( HttpServletRequest request,HttpServletResponse response,@PathVariable String needId) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request, response);//自动客户信息
 		List<BsUserInfor> userList = userInforService.selectUserInforInNeedInfor(needId);
		Map rsModel = new HashMap();
		rsModel.put("sysMonth", DateUtil.getSystemTimeBySomeFormat("yyyyMM"));
		rsModel.put("userList", userList);
 		context.buildAppResponse(rsModel);
	}
	/**
	 * 查询产能信息 
	 */
	@RequestMapping("/page/cap/{needId}/selectCapacity.do")
	public void selectCapacityAll( HttpServletRequest request,HttpServletResponse response,@PathVariable String needId) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request, response);//自动客户信息
 		List<BsCapacityAll> capCityList = capService.selectCapacityAll(needId);
		List<BsCapacityDstbt> detailList = capService.selectCapacityDstbt(needId) ;
 		Map rsModel = new HashMap();
 		rsModel.put("mainMap", capCityList.get(0));
 		rsModel.put("detailList",detailList);
		context.buildAppResponse(rsModel);
	}
 	/**
	 * 新增产能信息 
	 */
	@RequestMapping("/page/cap/addCapacity.do")
	public void addCapacity( HttpServletRequest request,HttpServletResponse response) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request, response);//自动客户信息
		BsCapacityDstbt capaCity = context.obtainReqModel(BsCapacityDstbt.class);
 		//1、需求报工的人员集合
 		List<BsUserInfor> userList = userInforService.selectUserInforInNeedInfor(capaCity.getNeedId());
 		boolean contantFlag = false;
 		for(BsUserInfor bean:userList){
 			if(capaCity.getUserNo().equals(bean.getUserNo())){
 				contantFlag = true;
 			}
 		}
 		if(contantFlag==false){
 			throw new JTransException("1001", "员工:"+capaCity.getUserName()+"报工范围内，不能分配产能!");
 		}

		//2、领取时间应该小于或等于评估时间
 		List<BsCapacityAll> capCityList = capService.selectCapacityAll(capaCity.getNeedId());
 		BsCapacityAll capCityMain = capCityList.get(0);
 		Double sumCapHour = Double.parseDouble(capCityMain.getSumCapacityHour());
 		Double needTime = Double.parseDouble(capCityMain.getNeedTime());
 		if(needTime<=0){
 			throw new JTransException("1001", "评估工时为0，需求评估工时异常了");
 		}
 		Double capHour = Double.parseDouble(capaCity.getCapacityHour());
 		if(sumCapHour+capHour>needTime){
 			throw new JTransException("1001", "分配的产能大于评估总工时了！请核对后分配！");
 		}
 		
 		//3、判断是否已经冻结
 		int freeflag= this.capService.selectUserFreeze(capaCity.getUserNo(),capaCity.getCapacityMonth());
 		if(freeflag>0){
 			throw new JTransException("1001", "对不起！"+capaCity.getCapacityMonth()+"月份的数据已经冻结，只能分配下个月的产能了！");
 		}
 		
 		//一个用户、一个需求、一个月份只能分配一次。
 		List<BsCapacityDstbt> personList = this.capService.selectCapacityPerson(capaCity);
 		if(personList.size()>0){
 			throw new JTransException("1001", "已经分配产能了，不要重复哦！");
 		}
 		
 		BsUserInfor user = context.getUserInfo();
		capaCity.setCapacityId(JRandomUtil.getUUID());
		capaCity.setDstbtMan(user.getUserNo());
 		int i = capService.insertCapInfor(capaCity);
		if(i==0){
		  throw new  JTransException("1001","添加失败"); 
		}
 		Map rsModel = new HashMap();
		context.buildAppResponse(rsModel);
	}
  
	/**
	 * 修改产能信息 
	 */
	@RequestMapping("/page/cap/updateCapacity.do")
	public void updateCapacity( HttpServletRequest request,HttpServletResponse response) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request, response);//自动客户信息
		BsCapacityDstbt capaCity = context.obtainReqModel(BsCapacityDstbt.class);
 		//1、需求报工的人员集合
		//2、领取时间应该小于或等于评估时间
 		BsUserInfor user = context.getUserInfo();
 		capaCity.setDstbtMan(user.getUserNo());
 		int i = capService.updateCapInfor(capaCity);
		if(i==0){
		  throw new  JTransException("1001","修改失败"); 
		}
 		Map rsModel = new HashMap();
		context.buildAppResponse(rsModel);
	}
  
	/**
	 * 删除产能信息 
	 */
	@RequestMapping("/page/cap/deleteCapacity.do")
	public void deleteCapacity( HttpServletRequest request,HttpServletResponse response) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request, response);//自动客户信息
		BsUserInfor user = context.getUserInfo();
		BsCapacityDstbt capaCity = context.obtainReqModel(BsCapacityDstbt.class);
		BsCapacityDstbt capBean = capService.selectCapacityById(capaCity);
		if(capBean==null){
			 throw new  JTransException("1001","删除失败,该条记录已不存在，请刷新！"); 
		}
		if("1".equals(capBean.getFreezeData())){//记录为冻结状态
			 throw new  JTransException("1001","删除失败,该条记录已冻结不能删除！");
		}
		if(!capBean.getDstbtMan().equals(user.getUserNo())){
			 throw new  JTransException("1001","删除失败,该条记录不是您分配的，请找分配人员删除！");
		}
   		int i = capService.deleteCapInfor(capaCity);
		if(i==0){
		  throw new  JTransException("1001","删除失败"); 
		}
 		Map rsModel = new HashMap();
		context.buildAppResponse(rsModel);
	}
	
}
